package client.comm.baoding.adapter

import android.content.Context
import android.content.Intent
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.databinding.DataBindingUtil
import androidx.paging.LoadState
import androidx.paging.LoadStateAdapter
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import client.comm.baoding.R
import client.comm.baoding.api.bean.GoodsIndex
import client.comm.baoding.api.bean.UserInfo
import client.comm.baoding.databinding.*
import client.comm.baoding.ui.TabOne2Activity
import client.comm.baoding.ui.TxActivity
import client.comm.baoding.ui.Zz2Activity
import client.comm.baoding.widget.HorizontalPageLayoutManager
import client.comm.baoding.widget.PagingScrollHelper
import client.comm.commlib.base.PagingViewHoder
import client.comm.commlib.base.SyBannerAdapter
import com.bumptech.glide.RequestManager
import com.bumptech.glide.request.RequestOptions
import com.youth.banner.indicator.CircleIndicator
import client.comm.commlib.getScreenWidth

class SyPagingHeaderAdapter(val manager: RequestManager, val context: Context) :
    LoadStateAdapter<PagingViewHoder>() {

    var goodsIndex: GoodsIndex? = null
    var userInfo: UserInfo? = null

    override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): PagingViewHoder {
        return PagingViewHoder(
            DataBindingUtil.inflate(
                LayoutInflater.from(parent.context),
                R.layout.pagingitem_syheader,
                parent,
                false
            )
        ).apply {
            val itemBinding = this.binding as PagingitemSyheaderBinding
            itemBinding.event = this@SyPagingHeaderAdapter
//            itemBinding.rvJx.setIndicator(CircleIndicator(context))

        }
    }

    override fun onBindViewHolder(holder: PagingViewHoder, loadState: LoadState) {

        goodsIndex?.let {
            val binding = holder.binding as PagingitemSyheaderBinding
            binding.event = this

            val width = getScreenWidth(context)
            val params: ViewGroup.LayoutParams = binding.banner.getLayoutParams()
            with(binding) {
                params.height = (width / 2.6f).toInt()//2.8、2.25f
                banner.setLayoutParams(params)
                banner.setIndicator(CircleIndicator(context))
                myMarquee.isSelected = true

                it.article_list?.let {
                    if (it.size > 0)
                        myMarquee.text = it[0].title
                }
            }

            val list = it.banner_list
            binding.banner.setAdapter(object : SyBannerAdapter<GoodsIndex.Banner>(list) {
                override fun onBindView(
                    holder: BannerViewHolder?,
                    data: GoodsIndex.Banner?,
                    position: Int,
                    size: Int
                ) {
                    manager.load(data?.image_url)
                        .apply(RequestOptions().error(R.mipmap.ic_launcher))
                        .into(holder!!.imageView)
                }
            })
            binding.banner.start()

//            if (it.jx_goods_list.isNotEmpty()) {
//                var adapter = JxGood2Adapter(context, manager) //设置适配器
//                adapter.submitList(it.jx_goods_list)
//                binding.rvJx.setAdapter(adapter, false)
//                binding.rvJx.start()
//
//            }

            userInfo?.let {
                binding.userInfo = it
            }
        }


    }

    fun goHz() {
        context.startActivity(Intent(context, Zz2Activity::class.java).apply {
            putExtra("index", 1)
        })
    }

    fun goYe() {
        context.startActivity(Intent(context, TxActivity::class.java))
    }

    var notice: ((ArrayList<GoodsIndex.Article>) -> Unit)? = null

    fun goNotice() {
        notice?.invoke(goodsIndex!!.article_list)
    }

    override fun onViewAttachedToWindow(holder: PagingViewHoder) {
        super.onViewAttachedToWindow(holder)
        val lp: ViewGroup.LayoutParams = holder.itemView.getLayoutParams()
        if (lp != null && lp is StaggeredGridLayoutManager.LayoutParams
        ) {
            lp.isFullSpan = true
        }
    }

    // true显示无论什么状态都显示
    override fun displayLoadStateAsItem(loadState: LoadState): Boolean {
        return super.displayLoadStateAsItem(loadState) || (loadState.endOfPaginationReached)
    }

    //会员专享
    fun onZone0() {
        val id = goodsIndex!!.category_btn[0].id
        if (id.isEmpty()) {
            return
        }
        context.startActivity(Intent(context, TabOne2Activity::class.java).apply {
            putExtra("title_name", "会员专享")
            putExtra("cate_id", id)
            putParcelableArrayListExtra(
                "banner",
                goodsIndex!!.banner_list_1 as java.util.ArrayList<out Parcelable>
            )
        })
    }

    //自营专区
    fun onZone1() {
        val id = goodsIndex!!.category_btn[1].id
        if (id.isEmpty()) {
            return
        }
        context.startActivity(Intent(context, TabOne2Activity::class.java).apply {
            putExtra("title_name", "精选好物")
            putExtra("cate_id", id)
            putParcelableArrayListExtra(
                "banner",
                goodsIndex!!.banner_list_2 as java.util.ArrayList<out Parcelable>
            )
        })

    }

    //日常专区
    fun onZone2() {
        val id = goodsIndex!!.category_btn[2].id
        if (id.isEmpty()) {
            return
        }
        context.startActivity(Intent(context, TabOne2Activity::class.java).apply {
            putExtra("title_name", "日常专区")
            putExtra("cate_id", id)
            putParcelableArrayListExtra(
                "banner",
                goodsIndex!!.banner_list_3 as java.util.ArrayList<out Parcelable>
            )

        })
    }

    //积分专区
    fun onZone3() {
        if (goodsIndex!!.category_btn.size >= 4) {
            goodsIndex!!.category_btn[3]?.let {
                val id = it.id
                if (id.isEmpty()) {
                    return
                }
                context.startActivity(Intent(context, TabOne2Activity::class.java).apply {
                    putExtra("title_name", "消费专区")
                    putExtra("cate_id", id)
                    putParcelableArrayListExtra(
                        "banner",
                        goodsIndex!!.banner_list_4 as java.util.ArrayList<out Parcelable>
                    )
                })
            }
        }

    }

}